package com.hanlink.dsp.ui.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edge.pbxdsp.R;

/*import butterknife.BindView;
import butterknife.ButterKnife;*/

/**
 * Created by zhb on 2017/11/4.
 */

public class SummingHighCH extends LinearLayout implements Checkable {

    //@BindView(R.id.btn_negative)
    ImageButton mBtnNegative;
    //@BindView(R.id.btn_positive)
    ImageButton mBtnPositive;
    //@BindView(R.id.checked_text_summ_high_ch_value)
    TextView mCheckTextViewCh;
    private boolean mChecked = false;

    private OnCheckedChangeListener onCheckedChangeListener;

    @Override
    public void setChecked(boolean checked) {

//        if (mChecked == checked) {
//            return;
//        }
        mChecked = checked;
        refreshCheckStatus();
        if (onCheckedChangeListener != null) {
            onCheckedChangeListener.onCheckedChanged(this, mChecked);
        }

    }

    private void refreshCheckStatus() {
        mCheckTextViewCh.setTextColor(mChecked ? getContext().getResources().getColor(com.hanlink.res.R.color.summing_ch_checked_color) : getContext().getResources().getColor(com.hanlink.res.R.color.white));
        mBtnNegative.setImageResource(mChecked ? com.hanlink.res.R.drawable.summing_btn_negative_checked : com.hanlink.res.R.drawable.summing_btn_negative_normal);
        mBtnPositive.setImageResource(mChecked ? com.hanlink.res.R.drawable.summing_btn_positive_checked : com.hanlink.res.R.drawable.summing_btn_positive_normal);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(SummingHighCH view, boolean isChecked);
    }

    public SummingHighCH(Context context) {
        super(context);
        init();
    }

    public SummingHighCH(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SummingHighCH(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setup(int tag, String value, boolean isChChecked) {
        setTag(tag);
        mCheckTextViewCh.setText(value);
        setChecked(isChChecked);
    }


    private void init() {
        View v = View.inflate(getContext(), R.layout.summint_high_ch, this);
        //ButterKnife.bind(this, this);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });

        mBtnNegative = v.findViewById(R.id.btn_negative);
        mBtnPositive = v.findViewById(R.id.btn_positive);
        mCheckTextViewCh = v.findViewById(R.id.checked_text_summ_high_ch_value);


    }


    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mCheckTextViewCh.setTextColor(enabled ? getContext().getResources().getColor(com.hanlink.res.R.color.white) : getContext().getResources().getColor(com.hanlink.res.R.color.summing_ch_disable_color));
        mBtnNegative.setImageResource(enabled ? com.hanlink.res.R.drawable.summing_btn_negative_selector : com.hanlink.res.R.drawable.summing_btn_negative_disable);
        mBtnPositive.setImageResource(enabled ? com.hanlink.res.R.drawable.summing_btn_postive_selector : com.hanlink.res.R.drawable.summing_btn_positive_disable);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }
}
